<?php

namespace app\web\controller;
use app\web\model\LineCouponModel;
use app\web\model\LineMemberModel;
use app\web\model\LineCouponrecordModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 优惠券管理
 */
class LineCouponController extends StoreBaseController
{
 	protected $model;
	protected $membermodel;
	protected $recordmodel;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new LineCouponModel();
		$this->membermodel = new LineMemberModel();
		$this->recordmodel = new LineCouponrecordModel();
    }
    /**
     * 优惠券管理
     * @author [lx] 
     */
    public function index( $page = 1){
    	if(request()->isPost()){
		 	$param = input('post.');
			//优惠券名称
			if($param['name']){
				$where['name'] = ['like', '%'.$param['name'].'%'];
			}			
	 	}		
    	$where['store_id']= session('storeInfo')['id'];
        $list = $this->model->where($where)->order('add_time desc')->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     * 添加
     * @author [lx] 
     */
    public function add()
    {
    	$storeinfo = session('storeInfo');	
        if(request()->isAjax()){
        	$param=input('post.');
			//所属店铺id
			$param['store_id'] = $storeinfo['id'];			
			$param['store_name'] = $storeinfo['store_name'];
			$param['add_time'] = now_time();
			//是否叠加使用
			$param['is_over'] = input('is_over') ? true : false;	
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('line_coupon/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');  
			//是否叠加使用
			$param['is_over'] = input('is_over') ? true : false;	  
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                $this->success('修改成功', url('line_coupon/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->where('id',$id)->find());
        return $this->fetch();
    }
	
	 /**
     * [grant 会员选择]
     * @author [lx] 
     */
    public function grant()
    {
    	if(request()->isPost()){
		 	$param = input('post.');
			//会员名称
			if($param['membername']){
				$where['membername'] = ['like', '%'.$param['membername'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$where['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
			//会员卡号
			if($param['member_no']){
				$where['member_no'] = ['like', '%'.$param['member_no'].'%'];
			}	
			//有效期
			if($param['start_time']&&$param['end_time']){
				$where['validity_date'] = ['between time', [$param['start_time'],$param['end_time']]];
			}		 				
	 	}
		$id = input('param.id');
    	$where['store_id']= session('storeInfo')['id'];
        $list = $this->membermodel->where($where)->order('reg_date desc')->paginate(10);
		$this->assign('couponid',$id);
        return $this->fetch('grant', ['list' => $list]);
    }
	
	/**
     * [grantclick 分发操作 $id 会员id $grantid 优惠券编号  ]
     * @author [lx] 
     */
    public function grantclick($id,$couponid)
    {
    	$storeinfo = session('storeInfo');	  	
	 	$param = input('post.');
		$param['coupon_id'] = $couponid;
		$param['coupon_no'] = order_create_no();
		//所属店铺id
		$param['store_id'] = $storeinfo['id'];			
		$param['store_name'] = $storeinfo['store_name'];
		$param['grant_time'] = now_time();
		$param['overdue_time'] = date("Y-m-d 23:59:59",strtotime("+1 year"));
		$memberlist = $this->membermodel->find($id);
		$param['member_id'] = $memberlist['id'];
		$param['member_no'] = $memberlist['member_no'];
     	if ($this->recordmodel->allowField(true)->save($param) !== false) {
     		$this->model->allowField(true)->save(['is_grant'=>1],['id'=>$couponid]);
            $this->success('发放成功', url('line_coupon/index'));
        } else {
            $this->error('发放失败');
        }
        return $this->fetch();
    }

	 /**
     * [grantrecord 优惠券分发记录]
     * @author [lx] 
     */
    public function grantrecord()
    {
		$id = input('param.id');
        $list = $this->recordmodel->where('coupon_id',$id)->order('grant_time desc')->paginate(10);
        return $this->fetch('grantrecord', ['list' => $list]);
    }

    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}